#include "vmsg_meta.h"
//#include <iostream>

vmsg_meta::vmsg_meta()
    :_count(0),_last_count(0)
{
    _last_time = now();
}

unsigned long long int vmsg_meta::now() const //now time coune from epoch
{
    unsigned long long int ret = 0;
    auto tp_now = std::chrono::high_resolution_clock::now();
    ret = std::chrono::duration_cast<std::chrono::milliseconds>(
            tp_now.time_since_epoch()).count();

    return ret;
}


vmsg_meta::~vmsg_meta()
{

}

void vmsg_meta::set_name(const std::string name)
{
    _name = name;
}

void vmsg_meta::increase_count()
{
    ++_count;
}

void vmsg_meta::increase_count(unsigned long long count)
{
    _count+=count;
}

unsigned long long  vmsg_meta::elapsed() const //return ms
{
    return now() - _last_time;
}

unsigned long long vmsg_meta::tps()
{
    double ret = 0;
    auto tp_now = now();
    auto step_elapsed = tp_now - _last_time;
    if(step_elapsed == 0)
    {
        ret = 0;
    }
    else
    {
        double step_count = static_cast<double>(_count-_last_count);
        ret = step_count/step_elapsed; 
        ret*=1000; //change time unit

        //std::cout<<"step count:"<<step_count
        //    <<",el:"<<step_elapsed
        //    <<",ret:"<<ret
        //    <<std::endl;
    }
    _last_count = _count;
    _last_time = tp_now;

    auto tmp = static_cast<unsigned long long >(ret);
    if(ret - static_cast<double>(tmp) >= 0.44445)
    {
       ++ret;
    }
    else
    {
       ret = tmp;
    }

    return ret;
}




